package controller;

import Utils.dateUtil;
import com.smart.domain.Express;
import com.smart.infer.userPubInt;
import com.smart.service.selectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
public class personalController {
    @Autowired
    private selectService ss;
   @Autowired
    private userPubInt mapper2;

    @RequestMapping("/toPersonal.html")
    public ModelAndView toperson(HttpServletRequest request) {
        String id = (String) request.getSession().getAttribute("userid");
        List list = ss.selectMy(id);//查询我的订单
        dateUtil change=new dateUtil();
        List l=change.changeStatus(list);
        List list2 =ss.selectMyLeader(id);//查询我的揽件
        int score=ss.findscore(id);//查询我的积分
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("score",score);
        modelAndView.addObject("name", id);
        modelAndView.addObject("list", l);
        modelAndView.addObject("list2",list2);
        modelAndView.setViewName("personCenter");
        return modelAndView;
    }
//    @RequestMapping("addExpress.html")
//    public ModelAndView add(HttpServletRequest request) throws UnsupportedEncodingException {
//        request.setCharacterEncoding("UTF-8");
//        String id = (String) request.getSession().getAttribute("userid");
//        String e_number=new String(request.getParameter("e_number").getBytes("ISO-8859-1"),"UTF-8");
//        String e_code=new String(request.getParameter("e_code").getBytes("ISO-8859-1"),"UTF-8");
//        String e_esc=new String(request.getParameter("esc").getBytes("ISO-8859-1"),"UTF-8");
//        String e_addressee=new String(request.getParameter("e_addressee").getBytes("ISO-8859-1"),"UTF-8");
//        String e_address=new String(request.getParameter("e_address").getBytes("ISO-8859-1"),"UTF-8");
//        String e_tellnumber=new String(request.getParameter("e_tellnumber").getBytes("ISO-8859-1"),"UTF-8");
//        dateUtil date=new dateUtil();
//        String dateString=date.date();
//        Express express=new Express();
//        express.setE_number(e_number);express.setE_code(e_code);express.setE_esc(e_esc);
//        express.setE_time(dateString);express.setE_addressee(e_addressee);express.setE_address(e_address);
//        express.setE_tellnumber(e_tellnumber);express.setE_user(id);express.setE_singer("无");express.setE_status("0");
//        express.setE_singernum("无");express.setE_leadtime("无");express.setUserid("无");
//        System.out.println(express);
//        mapper2.insert(express);
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.setViewName("personCenter");
//        return modelAndView;
//    }
    @RequestMapping("addExpress.html")
   public ModelAndView add(HttpServletRequest request) throws UnsupportedEncodingException {//添加订单
        request.setCharacterEncoding("UTF-8");
        String id = (String) request.getSession().getAttribute("userid");
        String e_number=request.getParameter("e_number");
        String e_code=request.getParameter("e_code");
        String e_esc=request.getParameter("esc");
        String e_addressee=request.getParameter("e_addressee");
        String e_address=request.getParameter("e_address");
        String e_tellnumber=request.getParameter("e_tellnumber");
        dateUtil date=new dateUtil();
        String dateString=date.date();
        int score=ss.findscore(id);//查询我的积分
        String massage="";
        if(score>=10){
            Express express=new Express();
            express.setE_number(e_number);express.setE_code(e_code);express.setE_esc(e_esc);
            express.setE_time(dateString);express.setE_addressee(e_addressee);express.setE_address(e_address);
            express.setE_tellnumber(e_tellnumber);express.setE_user(id);express.setE_singer("无");express.setE_status("0");
            express.setE_singernum("无");express.setE_leadtime("无");express.setUserid("无");
            ss.updateScoreDown(id);
            System.out.println(express);
            mapper2.insert(express);
            massage+="添加订单成功！请返回个人中心点击“我的订单查看”";
        }else{
            massage+="您的积分太少了呦，无法添加订单，请先去赚积分哟";
        }

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("Massage",massage);
        modelAndView.setViewName("addOrderSuccess");
        return modelAndView;
    }
}